package server.model.administration;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import server.model.meeting.Group;
import server.model.meeting.Participant;
import server.model.meeting.User;
import server.model.meeting.UserInfo;
import server.model.remotes.IAdmin;
import server.model.remotes.IMeetingService;

public class Admin extends UnicastRemoteObject implements IAdmin {
	
	private transient ArrayList<User> users;	
	private ArrayList<Participant> participants;
	private ArrayList<Group> group;
	private MeetingService _meetingService;
	
	public Admin()throws RemoteException{		
		super();
		_meetingService = new MeetingService(this);
		users = new ArrayList<User>(10);	
		participants = new ArrayList<Participant>(10);	
		group = new ArrayList<Group>(10);
		
		/**
		 * Testuser (test-test)
		 */
		registerUser("test","test","");
		String[] users = {"test" , "Oktay"};
		String[] groupUsers = {};
		createGroup("groep 1", users);
		
		//addTestUsers();
	}
	
	/*private void addTestUsers() throws RemoteException{
		registerUser("David", "", "");
		registerUser("Rens", "", "");
		registerUser("Steef", "", "");
		registerUser("Sander", "", "");
		registerUser("Ruud", "", "");
		registerUser("Oktay", "", "");
		registerUser("Bert", "", "");
		registerUser("Piet", "", "");
		registerUser("Hein", "", "");
		registerUser("John", "", "");
		registerUser("Frank", "", "");
		//registerUser("Albert", "", "");
		registerUser("Henk", "", "");
		registerUser("Merijn", "", "");
		registerUser("Michiel", "", "");
		registerUser("Erik", "", "");
		
		
		String[] users = {"David", "rens", "Steef"};
		createGroup("groep 1", users);
		String[] users1 = {"David", "rens", "Steef"};
		createGroup("groep 2", users1);
		String[] users2 = {"Ruud", "Oktay", "Piet", "John"};
		createGroup("groep 3", users2);
		String[] users3 = {"Henk", "Piet", "Ruud", "Frank"};
		createGroup("groep 4", users3);
*/
	
	public boolean registerUser(String name, String password, String email)throws RemoteException {
		
		if (getUserByName(name) != null){
			return false;
		}
	
		User user = new User();		
		user.setName(name);
		user.setPassword(password);	
		
		UserInfo userInfo = new UserInfo();		
		userInfo.setEmail(email);			

		user.setUserInfo(userInfo);		
	
		users.add(user);		
		
		return true;
	}
	public void deleteUser(){
		users.removeAll(users);
	}
	public void deleteParticipant(){
	 participants.removeAll(participants);
	}
	
	public IMeetingService loginUser(String name, String password) throws RemoteException{
		User user = getUserByName(name);
		
		if (user == null){
			return null;
		}

		if (user.checkPassword(password)){
			return (IMeetingService)_meetingService;
		}else{
			return null;
		}
	}
	
	private User getUserByName(String name) {
		Iterator<User> userIterator = users.iterator();
		while(userIterator.hasNext()){		
			User currentUser = userIterator.next();	
			if (name.matches(currentUser.getName())){
				return currentUser;			
			}
		}		
		return null;
	}
	
	public Participant getParticipantByName(String name) {
		Iterator<Participant> participantIterator = participants.iterator();
		while(participantIterator.hasNext()){		
			Participant currentParticipant = participantIterator.next();	
			if (name.matches(currentParticipant.getName())){
				System.out.println(currentParticipant);
				return currentParticipant;			
			}
		}		
		return null;
	}
	
	public ArrayList<User> getUsers()throws RemoteException{
		return users;	
	}
	
	public boolean createGroup(String name, String[] usernames)throws RemoteException {
		Participant participant = getParticipantByName(name);
		
		if (participant != null){
			return false;
		}		
		
		Group group = new Group();
		group.setName(name);
			
		for(int i=0; i<usernames.length; i++){
			if(getUserByName(usernames[i]) != null){
				group.addParticipant(getUserByName(usernames[i]));
			}
		}	
		
		participants.add(group);
		return true;
	}

	public ArrayList<Participant> getParticipants() throws RemoteException {
		return participants;	
	}

	@Override
	public ArrayList<Participant> getGroups() throws RemoteException {
		ArrayList <Participant>groups = new ArrayList();
		for(int i = 0; i < participants.size(); i++){
			if(participants.get(i).getClass().toString().equalsIgnoreCase("class server.model.meeting.Group")){
				groups.add(participants.get(i));
			}
		}
		return groups;
	}
}
